package cn.duli.xianer.controller;


import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.Users;
import cn.duli.xianer.service.FileService;
import cn.duli.xianer.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private FileService fileService;

    /**
     * 根据id查询此用户
     * @param uId
     * @return
     */
    @GetMapping("/findById/{uId}")
//    @RequiresRoles(logical = Logical.OR, value = {"admin", "user", "business"})
    public MyResult findById(@PathVariable("uId") Integer uId) {
        Users user = usersService.findById(uId);
        return new MyResult(200, "根据id查询此用户信息成功",null,null,user,null);
    }

    /**
     * 根据用户名回显用户个人信息
     * @param username
     * @return
     */
    @GetMapping("/findByUsername")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "user", "business"})
    public MyResult findByUsername(@RequestParam("username") String username) {
        Users user = usersService.findByUsername(username);
        return new MyResult(200, "根据用户名回显用户个人信息成功",null,null,user,null);
    }

    /**
     * 修改用户个人信息
     * @param user
     * @return
     */
    @RequestMapping("/update")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "user", "business"})
    public MyResult update(Users user) {
        usersService.update(user);
        return new MyResult(200, "修改用户个人信息成功",null,null,null,null);
    }

    /**
     * 修改用户个人头像并回显用户信息
     * @param user
     * @param upFile
     * @return
     */
    @RequestMapping("/updateAvatar")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "user", "business"})
    public MyResult updateAvatar(@RequestParam(value = "file",required = false) MultipartFile upFile, Users user) throws IOException {
        System.out.println("我进来了");
        System.out.println(upFile.getOriginalFilename());
        String imgUrl = fileService.uploadOneFile(upFile);
        user.setuAvatar(imgUrl);
        usersService.updateAvatar(user);
        Users newUser = usersService.findById(user.getuId());
        return new MyResult(200, "修改用户个人头像成功",null,null,newUser,null);
    }

    /**
     * 根据id删除此用户
     * @param uId
     * @return
     */
    @RequestMapping("/deleteById")
    @RequiresRoles("admin")
    public MyResult deleteById(@RequestParam Integer uId) {
        usersService.deleteById(uId);
        return new MyResult(200, "根据id删除此用户成功",null,null,null,null);
    }

    /**
     * 分页查询所有用户列表
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                            @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Users> pageInfo = new PageInfo<>(usersService.findAll());
        return new MyResult(200, "分页查询所有用户列表成功",null,null,null,pageInfo);
    }

    /**
     * 查询总用户数
     * @return
     */
    @RequestMapping("/findUsersCount")
    @RequiresRoles("admin")
    public MyResult findUsersCount() {
        int count = usersService.findUsersCount();
        return new MyResult(200, "查询总用户数成功",null,null,count,null);
    }

    /**
     * 查询入驻商家数
     * @return
     */
    @RequestMapping("/findBusinessCount")
    @RequiresRoles("admin")
    public MyResult findBusinessCount() {
        int count = usersService.findBusinessCount();
        return new MyResult(200, "查询入驻商家数成功",null,null,count,null);
    }

}
